<template>
  <div class="import-comtainer">
    <el-card>
      <h3>{{ title }}上传</h3>
      <el-alert title="警告提示的文案" type="warning" show-icon />
      <uploadExcel :on-success="successHandler" />
    </el-card>
  </div>
</template>

<script>
import uploadExcel from '../../components/uploadExcel'
import { formatExcelDate } from '../../utils/index'
import { importEmployees } from '../../api/uploadExcel'
export default {
  name: 'Import',
  components: {
    uploadExcel
  },
  computed: {
    /* eslint-disable */
    title() {
      switch (this.$route.query.type) {
        case 'employees':
          return '员工'
          break
        case 'departments':
          return '组织架构'
          break

        default:
          break
      }
    }
  },
  methods: {
    async successHandler(data) {
      //   console.log(data)
      const newArr = data.results.map(v => {
        v.correctionTime = formatExcelDate(v.correctionTime, '/')
        v.timeOfEntry = formatExcelDate(v.timeOfEntry, '/')
        return {
          ...v
        }
      })
      // console.log(newArr)
      await importEmployees(newArr)
      this.$message.success('导入成功')
      this.$router.push({
        path: '/employees'
      })
    }
  }
}
</script>

<style lang="scss">
.import-comtainer {
  padding: 20px;
  text-align: center;
}
</style>
